// OGoMojo
// Whitemice.MOGI.ZOGI.Payload
// License: LGPL
// Copyright: Whitemice Consulting, 2006
// Author(s): Adam Tauno Williams (awilliam@whitemice.org)
using System;
using System.Collections;
using CookComputing.XmlRpc;

namespace Whitemice.MOGI.ZOGI
{	
	public class Payload
	{
		XmlRpcStruct		hash;
		
		public Payload(XmlRpcStruct hash)
		{
			this.hash = hash;
		}
		
		internal XmlRpcStruct Hash
		{
			get { return hash; }
		}
		
		public bool HasCompanyValues()
		{
			if (hash.ContainsKey("_COMPANYVALUES"))
				return true;
			return false;
		}
		
		public bool HasPhoneValues()
		{
			if (hash.ContainsKey("_PHONES"))
				return true;
			return false;
		}
		
		public bool HasAddressValues()
		{
			if (hash.ContainsKey("_ADDRESSES"))
				return true;
			return false;
		}
		
		public ICollection CompanyValues()
		{
			if (HasCompanyValues())
				return ((XmlRpcStruct)hash["_COMPANYVALUES"]).Keys;
			return null;
		}
		
		public ICollection PhoneValues()
		{
			if (HasPhoneValues())
				return ((XmlRpcStruct)hash["_PHONES"]).Keys;
			return null;
		}		
		
		public ICollection AddressValues()
		{
			if (HasAddressValues())
				return ((XmlRpcStruct)hash["_ADDRESSES"]).Keys;
			return null;
		}		
		
		
		public bool HasCompanyValue(string attribute)
		{
			if (HasCompanyValues())
				if (((XmlRpcStruct)hash["_COMPANYVALUES"]).ContainsKey(attribute))
					return true;
			return false;
		}
		
		public bool HasPhoneValue(string attribute)
		{
			if (HasPhoneValues())
				if (((XmlRpcStruct)hash["_PHONES"]).ContainsKey(attribute))
					return true;
			return false;
		}		
		
		public bool HasAddressValue(string attribute)
		{
			if (HasAddressValues())
				if (((XmlRpcStruct)hash["_ADDRESSES"]).ContainsKey(attribute))
					return true;
			return false;
		}			
		
		public CompanyValue CompanyValue(string attribute)
		{
			if(HasCompanyValue(attribute))
				return new CompanyValue(
					new Payload((XmlRpcStruct)((XmlRpcStruct)hash["_COMPANYVALUES"])[attribute])
					);
			return null;
		}
		
		public PhoneNumber PhoneValue(string attribute)
		{
			if(HasPhoneValue(attribute))
				return new PhoneNumber(
					new Payload((XmlRpcStruct)((XmlRpcStruct)hash["_PHONES"])[attribute])
					);
			return null;
		}
		
		public Address AddressValue(string attribute)
		{
			if(HasAddressValue(attribute))
				return new Address(
					new Payload((XmlRpcStruct)((XmlRpcStruct)hash["_ADDRESSES"])[attribute])
					);
			return null;
		}
		
		public Array GetStructsForKey(string key)
		{
			ArrayList structs;
			
			structs = new ArrayList();
			if (hash.ContainsKey(key))
				if (hash[key] is Array)
					foreach(object content in (Array)hash[key])
						structs.Add((XmlRpcStruct)content);
			return structs.ToArray(typeof(XmlRpcStruct));
		}
		
		public Array GetPayloadsForKey(string key)
		{
			ArrayList	payloads;
			
			payloads = new ArrayList();
			if (hash.ContainsKey(key))
			{
				if (hash[key] is Array)
					foreach (object content in (Array)hash[key])
						payloads.Add(new Payload((XmlRpcStruct)content));
			}
			return payloads.ToArray(typeof(Payload));
		}
		
		public void SetValueForKey(string key, object value)
		{
			if (hash.ContainsKey(key))
					hash[key] = value;
				else
					hash.Add(key, value);
		}
		
		public object GetObjectForKey(string key)
		{
			if (hash.ContainsKey(key))
				return hash[key];
			return null;
		}
		
		public string GetStringForKey(string key)
		{
			if (hash.ContainsKey(key))
				if (hash[key] is string)
					return (string)hash[key];
				else
					return hash[key].ToString();
			return "";
		}
		
		public int GetIntForKey(string key)
		{
			int intVal;
			
			intVal = 0;
			if (hash.ContainsKey(key))
			  if (hash[key] is int)
			  	intVal =(int)hash[key];
			  	else try { intVal = Convert.ToInt32(hash[key]); }
			  				catch (Exception ex)
			  				{
			  				}
			return intVal;
		}
		
		public DateTime GetDateTimeForKey(string key)
		{
			if (hash.ContainsKey(key))
			{
				if(hash[key] is System.DateTime)
					return (DateTime)hash[key];
			}
			return DateTime.MinValue;
		}
		
		public Payload GetPayloadForKey(string key) 
		{
			if (hash.ContainsKey(key))
				return new Payload((XmlRpcStruct)hash[key]);
			return new Payload(new XmlRpcStruct());
		}
	}
}